package weeks;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.List;

import javax.swing.JOptionPane;

import com.google.gdata.client.*;
import com.google.gdata.data.*;
import com.google.gdata.util.*;

public class GBloggerServices {

	/**
	 * Public variables
	 */
	public static String username;
	public static String pass;
	public static String myEntryTitle;
	public static String loginStart = "Logging in....";
	public static String loginComplete = "Login Complete!";
	public static int blogCount = 0;

	public GBloggerServices() throws AuthenticationException, IOException {
		super();
	}
	
	public static void main(String[] args) throws AuthenticationException, IOException {
    	username = JOptionPane.showInputDialog(null, "enter username");
		pass = JOptionPane.showInputDialog(null, "enter pass");
		bloggerServices(username, pass);
	}

	/**
	 * Implements the blogger service code
	 * @throws AuthenticationException 
	 * @throws IOException 
	 */
	public static void bloggerServices(String user, String password) throws AuthenticationException, IOException {
		
		try{
			
			// Uses a URL modified with the username, logs in and gathers the information in the form of a list
			// Note, as of yet, this only gathers information from your default calendar.
			System.setProperty("https.proxyHost", "proxy4.ucd.ie");
    		System.setProperty("https.proxyPort", "8484");
			
    		System.out.println(loginStart);
    		
			GoogleService myService = new GoogleService("blogger", "Weeks - Blogger");
			myService.setUserCredentials(user, password);
			
			System.out.println(loginComplete);
			
			// Request the feed
		    final URL feedUrl = new URL("http://www.blogger.com/feeds/default/blogs");
		    Feed resultFeed = myService.getFeed(feedUrl, Feed.class);
		    
		    // Print the results
		    System.out.println();
		    System.out.println(resultFeed.getTitle().getPlainText());
		    for (int i = 0; i < resultFeed.getEntries().size(); i++) {
		      Entry entry = resultFeed.getEntries().get(i);
		      System.out.println("Title:	 	" + entry.getTitle().getPlainText());
		      System.out.println("Summary: 	 " + entry.getSummary().getPlainText());
		      System.out.println("id:		 " + entry.getId());
		      System.out.println("Updated:	  " + entry.getUpdated());
		      
		      System.out.println("Published: 	" + entry.getPublished().toUiString());
		      System.out.println();
		      blogCount++;
		    }
		    System.out.println();
		    System.out.println("Number of Blogs: " + blogCount);
		
		//Exception handling
    	} catch (MalformedURLException e) {
            System.out.println("Invalid URL");
        } catch (IOException e) {
            System.out.println("Unable to execute");
        } catch (AuthenticationException e){
        	System.out.println("Authentication Problem");
        } catch (ServiceException e){
        	System.out.println("Service Problem"); 
        }
    }

	/**
	 * @return the username
	 */
	public static String getUsername() {
		return username;
	}

	/**
	 * @return the myEntryTitle
	 */
	public static String getMyEntryTitle() {
		return myEntryTitle;
	}

	/**
	 * @return the blogCount
	 */
	public static int getBlogCount() {
		return blogCount;
	}

}
